﻿package {
	import org.cove.ape.CircleParticle;
	import org.cove.ape.Vector;
	import org.cove.ape.VectorForce;
	import org.cove.ape.CollisionEvent;
	import flash.utils.setTimeout;
	
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.utils.setTimeout;
	/**
	 * @author Cory
	 */
	 
	public class Bird extends CircleParticle {
		//private var _dx = 0;
		public static var WIN:String = "win";
		private static var nextPlayerNumber:uint = 1;
		
		public static var RADIUS:Number = 20;
		public static var DIAMETER:Number = RADIUS * 2;
		private var _startX:Number;
		private var _startY:Number;
		private var _playerNumber:uint;
		private var _lives:LivesCounter;
		private var _isAlive:Boolean = true;
		
		[Embed(source="joust.swf", symbol="StandRight")]
		public static var StandRight:Class;
		
		[Embed(source="joust.swf", symbol="StandLeft")]
		public static var StandLeft:Class;

		[Embed(source="joust.swf", symbol="FlyRightUp")]
		public static var FlyRightUp:Class;
		
		[Embed(source="joust.swf", symbol="FlyRightDown")]
		public static var FlyRightDown:Class;
		
		[Embed(source="joust.swf", symbol="FlyLeftUp")]
		public static var FlyLeftUp:Class;
		
		[Embed(source="joust.swf", symbol="FlyLeftDown")]
		public static var FlyLeftDown:Class;
		
		private var display:Sprite = new Sprite();
		private var pose:Sprite;
		
		private var left:Boolean = true;
		
		public function Bird(x:int, y:int) {
			super(x, y, RADIUS);
			_startX = x;
			_startY = y;
			addEventListener(CollisionEvent.COLLIDE, this.collisionHandler);
			_playerNumber = nextPlayerNumber++;
			_lives = new LivesCounter(Joust.STARTING_LIVES);
			
			setPose(new StandLeft());
			setDisplay(display, -(pose.width / 2), -(pose.height / 2));
			
			//sprite.addChild(pose);
		}
		
		private function endFlap():void {
			if (left)
				setPose(new FlyLeftDown());
			else
				setPose(new FlyRightDown());
		}
		
		private function setPose(sprite:Sprite):void {
			if (pose != null)
				display.removeChild(pose);
			pose = sprite;
			pose.scaleX = DIAMETER / sprite.width;
			pose.scaleY = DIAMETER / sprite.height;
			display.addChild(pose);
		}
		
		public function get playerNumber():uint {
			return _playerNumber;
		}
		
		public function get lives():LivesCounter {
			return _lives;
		}
		
		public function get isAlive():Boolean {
			return _isAlive;
		}
		
		public function flap():void {
			if (this.isAlive) {
				if (left)
					setPose(new FlyLeftUp());
				else
					setPose(new FlyRightUp());
				setTimeout(endFlap, 100);
				
			
				//velocity.plusEquals(new Vector(0, -5));
				addForce(new VectorForce(false, 0, -30));
				if (velocity.magnitude() > 1) velocity.multEquals(1 / velocity.magnitude());
			}
		}
		
		public override function update(dt2:Number):void {
			super.update(dt2);
			
		}
		
		public function goLeft():void {
			left = true;
			setPose(new FlyLeftDown());
			addForce(new VectorForce(false, -5, 0));
		}
		
		public function goRight():void {
			left = false;
			setPose(new FlyRightDown());
			addForce(new VectorForce(false, 5, 0));
		}
		
		private function collisionHandler(event:CollisionEvent):void {
			if (event.collidingItem is Bird) {
				var opponent:Bird = event.collidingItem as Bird;		
				if (this.isAlive && opponent.isAlive) {
					if (this.sprite.y < opponent.sprite.y + this.radius * .5) {
						// Win condition
						var killEvent:JoustEvent = new JoustEvent(JoustEvent.KILL);
						killEvent.winner = this;
						killEvent.loser = opponent;
						this.dispatchEvent(killEvent);
					}
				}
			}
		}
		public function kill():void {
			this.loseRider();
			this._isAlive = false;
			setTimeout(this.resetPosition, 3000);
		}
		private function loseRider():void {
			
		}
		private function resetPosition():void {
			this._isAlive = true;
			this.position = new Vector(this._startX, this._startY);
		}
	}
}
